﻿namespace Vdc.Calendar2.Designer
{
    using Vdc.Calendar2;
    using System;
    using System.Globalization;
    using System.Text;
    using System.Web.UI.Design;

    [CLSCompliant(true)]
    public sealed class CalendarDesigner : ControlDesigner
    {
        private int GetDate(DateTime date, Vdc.Calendar2.Calendar calendar)
        {
            return new CultureInfo(calendar.CultureName).Calendar.GetDayOfMonth(date);
        }

        public override string GetDesignTimeHtml()
        {
            Vdc.Calendar2.Calendar component = (Vdc.Calendar2.Calendar)base.Component;
            StringBuilder builder = new StringBuilder();
            builder.Append("<link rel='stylesheet' type='text/css' href='" + ((component.StyleFolder != string.Empty) ? component.StyleFolder : "styles/default") + "/style.css'>");
            if (!component.DatePickerMode)
            {
                DateTime dateFirstMonth = component.DateFirstMonth;
                builder.Append(string.Concat(new object[] { "<div class=\"", component.CSSCalendar, "\" style=\"width:", (component.MonthWidth + component.MonthMarginWidth) * component.Columns, "px;\">" }));
                builder.Append("<table border=\"0\" cellspacing=\"1\" cellpadding=\"2\">");
                builder.Append("<tr>");
                builder.Append("<td colspan=\"7\" align=\"center\" width=\"" + ((component.MonthWidth + component.MonthMarginWidth) * component.Columns) + "px\">");
                builder.Append("<table border=\"0\" width=\"100%\">");
                builder.Append("<tr>");
                builder.Append("<td align=\"left\" width=\"1%\">");
                builder.Append("<div class=\"" + component.CSSArrowLeft + "\">" + component.TextArrowLeft + "</div>");
                builder.Append("</td>");
                builder.Append("<td align=\"center\">");
                builder.Append("<div class=\"" + component.CSSTitle + "\">");
                builder.Append(component.TitleText);
                builder.Append(this.GetYearSelector(component));
                builder.Append(this.GetMonthSelector(component));
                builder.Append("</div>");
                builder.Append("</td>");
                builder.Append("<td align=\"right\" width=\"1%\">");
                builder.Append("<div class=\"" + component.CSSArrowRight + "\">" + component.TextArrowRight + "</div>");
                builder.Append("</td>");
                builder.Append("</tr>");
                builder.Append("</table>");
                builder.Append("</td>");
                builder.Append("</tr>");
                for (int i = 1; i <= component.Rows; i++)
                {
                    builder.Append("<tr>");
                    for (int j = 1; j <= component.Columns; j++)
                    {
                        DateTime time2 = new DateTime(dateFirstMonth.Year, dateFirstMonth.Month, 1);
                        while (Convert.ToInt32(time2.DayOfWeek) != component.FirstDayOfWeek)
                        {
                            time2 = time2.AddDays(-1.0);
                        }
                        builder.Append(string.Concat(new object[] { "<td style=\"width:", component.MonthWidth, "px;height:", component.MonthHeight, "px;\">" }));
                        builder.Append(string.Concat(new object[] { "<table class=\"", component.CSSMonth, "\" width=\"100%\" height=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin:", component.MonthMarginWidth / 2, "px\">" }));
                        builder.Append("<tr>");
                        string[] strArray = component.FullMonthNames.Split(new char[] { ',' });
                        builder.Append(string.Concat(new object[] { "<td align=\"center\" colspan=\"7\" class=\"", component.CSSMonthTitle, "\">", strArray[dateFirstMonth.Month - 1], ", ", dateFirstMonth.Year, "</td>" }));
                        builder.Append("</tr>");
                        builder.Append("<tr>");
                        string[] strArray2 = component.ShortDayNames.Split(new char[] { ',' });
                        for (int k = component.FirstDayOfWeek; k < (7 + component.FirstDayOfWeek); k++)
                        {
                            builder.Append("<td align=\"center\" style=\"width:14.2%;\" class=\"" + component.CSSMonthTitle + "\">" + strArray2[(k < 7) ? k : (k - 7)] + "</td>");
                        }
                        builder.Append("</tr>");
                        for (int m = 0; m < 6; m++)
                        {
                            builder.Append("<tr>");
                            for (int n = 1; n <= 7; n++)
                            {
                                if (((time2.Month == dateFirstMonth.Month) && ((time2 >= component.DateMin) || (component.DateMin == new DateTime(0L)))) && ((time2 <= component.DateMax) || (component.DateMax == new DateTime(0L))))
                                {
                                    string cSSDay = component.CSSDay;
                                    if (((time2.Day == this.GetDate(component.DateToday, component)) && (time2.Month == this.GetMonth(component.DateToday, component))) && (time2.Year == this.GetYear(component.DateToday, component)))
                                    {
                                        cSSDay = component.CSSDayToday;
                                    }
                                    for (int num6 = 0; num6 < component.SpecialDates.Count; num6++)
                                    {
                                        if (((component.SpecialDates[num6].Day == time2.Day) && (component.SpecialDates[num6].Month == time2.Month)) && (component.SpecialDates[num6].Year == time2.Year))
                                        {
                                            cSSDay = component.CSSDaySpecial;
                                        }
                                    }
                                    if (((time2.Day == this.GetDate(component.SelectedDate, component)) && (time2.Month == this.GetMonth(component.SelectedDate, component))) && (time2.Year == this.GetYear(component.SelectedDate, component)))
                                    {
                                        cSSDay = component.CSSDaySelected;
                                    }
                                    builder.Append("<td align=\"center\" style=\"width:14.2%;\" class=\"" + cSSDay + "\">");
                                    builder.Append(time2.Day);
                                    builder.Append("</td>");
                                }
                                else
                                {
                                    builder.Append("<td align=\"center\" style=\"width:14.2%;\" class=\"" + component.CSSDayDisabled + "\">");
                                    builder.Append(component.ShowOtherMonthDays ? time2.Day.ToString() : "");
                                    builder.Append("</td>");
                                }
                                time2 = time2.AddDays(1.0);
                            }
                            builder.Append("</tr>");
                        }
                        builder.Append("</table>");
                        builder.Append("</td>");
                        dateFirstMonth = dateFirstMonth.AddMonths(1);
                    }
                    builder.Append("</tr>");
                }
                builder.Append("</table>");
                builder.Append("</div>");
                builder.Append(this.GetTimeSelector(component));
            }
            return builder.ToString();
        }

        private int GetMonth(DateTime date, Vdc.Calendar2.Calendar calendar)
        {
            return new CultureInfo(calendar.CultureName).Calendar.GetMonth(date);
        }

        private string GetMonthSelector(Vdc.Calendar2.Calendar calendar)
        {
            string str = "";
            if (calendar.ShowMonthSelector)
            {
                int num;
                switch (calendar.MonthSelectorType)
                {
                    case SelectorType.DropDownList:
                        str = "<select>";
                        num = 0;
                        while (num < 12)
                        {
                            if (num == (this.GetMonth(calendar.DateFirstMonth, calendar) - 1))
                            {
                                str = str + "<option selected=\"true\">" + calendar.FullMonthNames.Split(new char[] { ',' })[num] + "</option>";
                            }
                            else
                            {
                                str = str + "<option>" + calendar.FullMonthNames.Split(new char[] { ',' })[num] + "</option>";
                            }
                            num++;
                        }
                        return (str + "</select>");

                    case SelectorType.HtmlList:
                        str = "<div style=\"display:inline\" class=\"" + calendar.CSSMonthSelectorTitle + "\">";
                        for (num = 0; num < 12; num++)
                        {
                            if (num == (this.GetMonth(calendar.DateFirstMonth, calendar) - 1))
                            {
                                str = str + calendar.FullMonthNames.Split(new char[] { ',' })[num];
                            }
                        }
                        return (str + "</div>");
                }
            }
            return str;
        }

        private string GetTimeSelector(Vdc.Calendar2.Calendar calendar)
        {
            StringBuilder builder = new StringBuilder();
            StringBuilder builder2 = new StringBuilder();
            StringBuilder builder3 = new StringBuilder();
            StringBuilder builder4 = new StringBuilder();
            if (calendar.ShowTimeSelector)
            {
                builder.Append("<div style=\"text-align:center;width:100%;\">");
                switch (calendar.TimeSelectorType)
                {
                    case SelectorType.DropDownList:
                    {
                        int num = 0;
                        while (num < 60)
                        {
                            if (num == calendar.SelectedDate.Minute)
                            {
                                builder2.Append("<option selected='true'>" + num.ToString("#00") + "</option>");
                            }
                            else
                            {
                                builder2.Append("<option>" + num.ToString("#00") + "</option>");
                            }
                            if (num == calendar.SelectedDate.Second)
                            {
                                builder3.Append("<option selected='true'>" + num.ToString("#00") + "</option>");
                            }
                            else
                            {
                                builder3.Append("<option>" + num.ToString("#00") + "</option>");
                            }
                            num++;
                        }
                        for (num = 0; num < 0x18; num++)
                        {
                            if (num == calendar.SelectedDate.Hour)
                            {
                                builder4.Append("<option selected='true'>" + num.ToString("#00") + "</option>");
                            }
                            else
                            {
                                builder4.Append("<option>" + num.ToString("#00") + "</option>");
                            }
                        }
                        if (calendar.ShowHourSelector)
                        {
                            builder.Append("<select>" + builder4 + "</select>");
                        }
                        if (calendar.ShowMinuteSelector)
                        {
                            if (calendar.ShowHourSelector)
                            {
                                builder.Append(":");
                            }
                            builder.Append("<select>" + builder2 + "</select>");
                        }
                        if (calendar.ShowSecondSelector)
                        {
                            if (calendar.ShowHourSelector || calendar.ShowMinuteSelector)
                            {
                                builder.Append(":");
                            }
                            builder.Append("<select>" + builder3 + "</select>");
                        }
                        break;
                    }
                    case SelectorType.HtmlList:
                    {
                        int num3;
                        int num4;
                        builder4.Append("<div style=\"display:inline;\" class=\"" + calendar.CSSHourSelectorTitle + "\">");
                        int num2 = 0;
                        while (num2 <= 0x18)
                        {
                            if (num2 == calendar.DateFirstMonth.Hour)
                            {
                                builder4.Append(num2.ToString("#00"));
                            }
                            num2++;
                        }
                        builder4.Append("</div>");
                        builder4.Append("<div style=\"display:inline;\">");
                        builder4.Append("<table cellpadding=\"0\" cellspacing=\"0\" class=\"" + calendar.CSSHourSelectorContainer + "\">");
                        builder4.Append("<tr><td class=\"" + calendar.CSSHourSelectorArrowUp + "\">&nbsp;</td></tr>");
                        for (num2 = 0; num2 <= 0x17; num2++)
                        {
                            if (num2 == calendar.DateFirstMonth.Hour)
                            {
                                builder4.Append("<tr class=\"" + calendar.CSSHourSelected + "\"><td>" + num2.ToString("#00") + "</td></tr>");
                            }
                            else if ((num2 <= 10) && (num2 >= 0))
                            {
                                builder4.Append("<tr class=\"" + calendar.CSSHourSelector + "\"><td>" + num2.ToString("#00") + "</td></tr>");
                            }
                            else
                            {
                                builder4.Append("<tr style=\"display:none\" class=\"" + calendar.CSSHourSelector + "\"><td>" + num2.ToString("#00") + "</td></tr>");
                            }
                        }
                        builder4.Append("<tr><td class=\"" + calendar.CSSHourSelectorArrowDown + "\">&nbsp;</td></tr>");
                        builder4.Append("</table>");
                        builder4.Append("</div>");
                        builder2.Append("<div style=\"display:inline;\" class=\"" + calendar.CSSMinuteSelectorTitle + "\">");
                        for (num3 = 0; num3 <= 0x3b; num3++)
                        {
                            if (num3 == calendar.DateFirstMonth.Minute)
                            {
                                builder2.Append(num3.ToString("#00"));
                            }
                        }
                        builder2.Append("</div>");
                        builder2.Append("<div style=\"display:inline;\">");
                        builder2.Append("<table cellpadding=\"0\" cellspacing=\"0\" class=\"" + calendar.CSSMinuteSelectorContainer + "\">");
                        builder2.Append("<tr><td class=\"" + calendar.CSSMinuteSelectorArrowUp + "\">&nbsp;</td></tr>");
                        for (num3 = 0; num3 <= 0x3b; num3++)
                        {
                            if (num3 == calendar.DateFirstMonth.Minute)
                            {
                                builder2.Append("<tr class=\"" + calendar.CSSMinuteSelected + "\"><td>" + num3.ToString("#00") + "</td></tr>");
                            }
                            else if ((num3 <= 10) && (num3 >= 0))
                            {
                                builder2.Append("<tr class=\"" + calendar.CSSMinuteSelector + "\"><td>" + num3.ToString("#00") + "</td></tr>");
                            }
                            else
                            {
                                builder2.Append("<tr style=\"display:none\" class=\"" + calendar.CSSMinuteSelector + "\"><td>" + num3.ToString("#00") + "</td></tr>");
                            }
                        }
                        builder2.Append("<tr><td class=\"" + calendar.CSSMinuteSelectorArrowDown + "\">&nbsp;</td></tr>");
                        builder2.Append("</table>");
                        builder2.Append("</div>");
                        builder3.Append("<div style=\"display:inline;\" class=\"" + calendar.CSSSecondSelectorTitle + "\">");
                        for (num4 = 0; num4 <= 0x3b; num4++)
                        {
                            if (num4 == calendar.DateFirstMonth.Second)
                            {
                                builder3.Append(num4.ToString("#00"));
                            }
                        }
                        builder3.Append("</div>");
                        builder3.Append("<div style=\"display:inline;\">");
                        builder3.Append("<table cellpadding=\"0\" cellspacing=\"0\" class=\"" + calendar.CSSSecondSelectorContainer + "\">");
                        builder3.Append("<tr><td class=\"" + calendar.CSSSecondSelectorArrowUp + "\">&nbsp;</td></tr>");
                        for (num4 = 0; num4 <= 0x3b; num4++)
                        {
                            if (num4 == calendar.DateFirstMonth.Second)
                            {
                                builder3.Append("<tr class=\"" + calendar.CSSSecondSelected + "\"><td>" + num4.ToString("#00") + "</td></tr>");
                            }
                            else if ((num4 <= 10) && (num4 >= 0))
                            {
                                builder3.Append("<tr class=\"" + calendar.CSSSecondSelector + "\"><td>" + num4.ToString("#00") + "</td></tr>");
                            }
                            else
                            {
                                builder3.Append("<tr style=\"display:none\" class=\"" + calendar.CSSSecondSelector + "\"><td>" + num4.ToString("#00") + "</td></tr>");
                            }
                        }
                        builder3.Append("<tr><td class=\"" + calendar.CSSSecondSelectorArrowDown + "\">&nbsp;</td></tr>");
                        builder3.Append("</table>");
                        builder3.Append("</div>");
                        if (calendar.ShowHourSelector)
                        {
                            builder.Append(builder4);
                        }
                        if (calendar.ShowMinuteSelector)
                        {
                            if (calendar.ShowHourSelector)
                            {
                                builder.Append(":");
                            }
                            builder.Append(builder2);
                        }
                        if (calendar.ShowSecondSelector)
                        {
                            if (calendar.ShowHourSelector || calendar.ShowMinuteSelector)
                            {
                                builder.Append(":");
                            }
                            builder.Append(builder3);
                        }
                        break;
                    }
                }
                if (calendar.DatePickerMode)
                {
                    builder.Append("<a class=\"" + calendar.CSSSelectTime + "\">" + calendar.TextSelectTime + "</a>");
                }
                builder.Append("</div>");
            }
            return builder.ToString();
        }

        private int GetYear(DateTime date, Vdc.Calendar2.Calendar calendar)
        {
            return new CultureInfo(calendar.CultureName).Calendar.GetYear(date);
        }

        private string GetYearSelector(Vdc.Calendar2.Calendar calendar)
        {
            string str = "";
            if (calendar.ShowYearSelector)
            {
                int num;
                int num2;
                switch (calendar.YearSelectorType)
                {
                    case SelectorType.DropDownList:
                        str = "<select>";
                        num = (calendar.DateMin == new DateTime(0L)) ? (this.GetYear(calendar.DateFirstMonth, calendar) - 10) : calendar.DateMin.Year;
                        num2 = (calendar.DateMax == new DateTime(0L)) ? (this.GetYear(calendar.DateFirstMonth, calendar) + 10) : calendar.DateMax.Year;
                        while (num <= num2)
                        {
                            object obj2;
                            if (num == this.GetYear(calendar.DateFirstMonth, calendar))
                            {
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<option selected=\"true\">", num, "</option>" });
                            }
                            else
                            {
                                obj2 = str;
                                str = string.Concat(new object[] { obj2, "<option>", num, "</option>" });
                            }
                            num++;
                        }
                        return (str + "</select>");

                    case SelectorType.HtmlList:
                    {
                        num = (calendar.DateMin == new DateTime(0L)) ? (this.GetYear(calendar.DateFirstMonth, calendar) - 10) : calendar.DateMin.Year;
                        num2 = (calendar.DateMax == new DateTime(0L)) ? (this.GetYear(calendar.DateFirstMonth, calendar) + 10) : calendar.DateMax.Year;
                        DateTime time = new DateTime(0L);
                        int num3 = (calendar.YearMinScroll == time.Year) ? (this.GetYear(calendar.DateFirstMonth, calendar) - 5) : calendar.YearMinScroll;
                        num3 = (num3 < num) ? num : num3;
                        time = new DateTime(0L);
                        int num4 = (calendar.YearMaxScroll == time.Year) ? (this.GetYear(calendar.DateFirstMonth, calendar) + 5) : calendar.YearMaxScroll;
                        num4 = (num4 > num2) ? num2 : num4;
                        str = "<div style=\"display:inline\" class=\"" + calendar.CSSYearSelectorTitle + "\">";
                        for (int i = num; i <= num2; i++)
                        {
                            if (i == this.GetYear(calendar.DateFirstMonth, calendar))
                            {
                                str = str + i;
                            }
                        }
                        return (str + "</div>");
                    }
                }
            }
            return str;
        }
    }
}

